<?php
/**
 * Modulos
 */

class Modulos extends Controller {
    public $lenguaje='spanish';

    function Modulos() {
        parent::Controller();
        if($this->session->userdata('perms')!='admin')
            redirect('/home/');
        $this->lang->load('general',$this->lenguaje);
    }

    function index() {
        $datos['titulo_pagina'] = 'Modulos';
        $this->load->view('admin/encabezado_admin.php', $datos);
        $this->load->view('admin/menu_admin.php');
        $this->load->view('admin/modulos.php');
        $this->load->view('admin/pie_admin.php');
    }


    function post() {
        $tabla       = "min_modulos";
        $page        = $this->input->post('page');
        $limit       = $this->input->post('rows');
        $sidx        = $this->input->post('sidx');
        $sord        = $this->input->post('sord');
        $_search     = $this->input->post('_search');
        $searchField = $this->input->post('searchField');
        $searchString= $this->input->post('searchString');

        $where = "";
        if ($_search=='true')
            $where = " WHERE $searchField LIKE '%$searchString%' ";


        if(!$sidx) $sidx =1;

        $sql    = "SELECT * FROM $tabla $where";
        //echo $sql;
        $query  = $this->db->query($sql);
        $result = $query->result();

        $count  = count($result);

        if( $count > 0 && $limit > 0) {
            $total_pages = ceil($count/$limit);
        } else {
            $total_pages = 0;
        }

        if ($page > $total_pages) $page=$total_pages;
        $start = $limit*$page - $limit;
        if($start <0) $start = 0;

        $sql    = "SELECT * FROM $tabla $where ORDER BY $sidx $sord LIMIT $start , $limit";
        $query  = $this->db->query($sql);
        $result = $query->result();


        header("Expires: Mon, 26 Jul 1997 05:00:00 GMT" );
        header("Last-Modified: " . gmdate( "D, d M Y H:i:s" ) . "GMT" );
        header("Cache-Control: no-cache, must-revalidate" );
        header("Pragma: no-cache" );
        header("Content-type: text/x-json");
        $i=0;
        $data['page']= $page;
        $data['total']= $total_pages;

        foreach ($query->result() as $row) {
            $data['rows'][$i]['id']= $row->id;
            $data['rows'][$i]['cell'][]= $row->id;
            $data['rows'][$i]['cell'][]= utf8_encode($row->nombre);
            $data['rows'][$i]['cell'][]= utf8_encode($row->texto_menu);
            $data['rows'][$i]['cell'][]= utf8_encode($row->empresa);
            $data['rows'][$i]['cell'][]= utf8_encode($row->estado);
            $data['rows'][$i]['cell'][]= utf8_encode($row->visible);
            $i++;
        }
        echo json_encode($data);
    }

    function guardar() {
        $tabla          = "min_modulos";
        $tabla2         = "min_permisos";
        $oper           = $this->input->post('oper');
        $id             = $this->input->post('id');
        $nombre         = $this->input->post('nombre');
        $texto_menu     = $this->input->post('texto_menu');
        $empresa        = $this->input->post('empresa');
        $estado         = $this->input->post('estado');
        $visible        = $this->input->post('visible');
        if($oper=='edit') {
            $query = $this->db->query("UPDATE $tabla  set nombre='".$nombre."', texto_menu='".$texto_menu."', empresa='".$empresa."', estado='".$estado."', visible='".$visible."' WHERE id=".$id);
            $query = $this->db->query("UPDATE $tabla2 set empresa='".$empresa."', modulo='".$nombre."' WHERE id_modulo=".$id." AND empresa='".$empresa."'");
            }elseif($oper=='add') {
            $query = $this->db->query("INSERT INTO $tabla  set nombre='".$nombre."', texto_menu='".$texto_menu."', empresa='".$empresa."', estado='".$estado."', visible='".$visible."'");
            }elseif($oper=='del') {
            $query = $this->db->query("DELETE FROM $tabla WHERE id=$id");
        }
    }


}
?>